%%
%% This is file 'bxcoloremoji.sty'.
%%
%% Copyright (c) 2017-2020 Takayuki YATO (aka. "ZR")
%%   GitHub:   https://github.com/zr-tex8r
%%   Twitter:  @zr_tex8r
%%
%% This package is distributed under the MIT License.
%%

%% package declarations
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{bxcoloremoji}[2020/06/21 v0.11]

%% preparatons
\def\bxce@pkgname{bxcoloremoji}
\def\bxce@error{\PackageError\bxce@pkgname}
\def\bxce@warn{\PackageWarning\bxce@pkgname}
\providecommand\bxDebug[1]{}

%% packages
\RequirePackage{etoolbox}
\RequirePackage{keyval}
\input{binhex.tex}

%% code guard
\edef\bxce@restore@codes{%
  \catcode33=\the\catcode33%
  \catcode34=\the\catcode34%
  \catcode39=\the\catcode39%
  \catcode43=\the\catcode43%
  \catcode45=\the\catcode45%
  \catcode46=\the\catcode46%
  \catcode47=\the\catcode47%
  \catcode58=\the\catcode58%
  \catcode59=\the\catcode59%
  \catcode60=\the\catcode60%
  \catcode62=\the\catcode62%
  \catcode63=\the\catcode63%
  \catcode96=\the\catcode96%
  \catcode126=\the\catcode126%
  \endlinechar=\the\endlinechar%
\relax}
\catcode33=12 % <!>
\catcode34=12 % <">
\catcode39=12 % <'>
\catcode43=12 % <+>
\catcode45=12 % <->
\catcode46=12 % <.>
\catcode47=12 % </>
\catcode58=12 % <:>
\catcode59=12 % <;>
\catcode60=12 % <<>
\catcode62=12 % <>>
\catcode63=12 % <?>
\catcode96=12 % <`>
\catcode126=13 % <~>
\AtEndOfPackage{%
  \bxce@restore@codes
  \let\bxce@restore@codes\relax}

%--------------------------------------- package options

%%<*> \coloremojidir : base directory
\newcommand*\coloremojidir{emoji_images/}
%% bxce@family : image family
\let\bxce@family\@undefined
%% bxce@bb : image bbox
\let\bxce@bb\relax
%% bxce@ext : image file extension
\let\bxce@ext\relax
%% bxce@scale : image scale
\def\bxce@scale{1}

%% keyval options
\define@key{bxce}{no-image}[]{%
  \let\bxce@family\relax
  \let\bxce@bb\relax
  \let\bxce@ext\relax
}
\define@key{bxce}{twemoji-pdf}[]{%
  \def\bxce@family{twemoji-pdf}%
  \def\bxce@bb{0 0 28.8 28.8}%
  \def\bxce@ext{pdf}%
}
\define@key{bxce}{twemoji-png}[]{%
  \def\bxce@family{twemoji-png}%
  \def\bxce@bb{0 0 72 72}%
  \def\bxce@ext{png}%
}
\define@key{bxce}{twitter}[]{%
  \def\bxce@family{twitter}%
  \def\bxce@bb{0 0 38 38}%
  \def\bxce@ext{pdf}%
}
\define@key{bxce}{hires}[]{%
  \def\bxce@family{hires}%
  \def\bxce@bb{0 0 11.338600 11.338600}%
  \def\bxce@ext{pdf}%
}
\define@key{bxce}{lowres}[]{%
  \def\bxce@family{lowres}%
  \def\bxce@bb{0 0 4.535430 4.535430}%
  \def\bxce@ext{pdf}%
}
\define@key{bxce}{scale}{%
  \def\bxce@scale{#1}%
}
\define@key{bxce}{basedir}{%
  \def\coloremojidir{#1}%
}
\DeclareOption*{%
  \expandafter\bxce@tmpa\CurrentOption\bxce@end
}
\def\bxce@tmpa#1\bxce@end{%
  \setkeys{bxce}{#1}%
}
\ProcessOptions*
\ifx\bxce@family\@undefined
  \setkeys{bxce}{twemoji-pdf}% default
\fi

%--------------------------------------- environment check

%% e-TeX check
\ifx\numdef\@undefined % etoolbox is aborted
  % etoolbox has already issued an error
  \bxce@warn{Package loading is aborted\@gobble}
\expandafter\endinput\fi\relax

%% \bxce@engine : engine type
\let\bxce@engine\relax
%% switch 'bxce@jaok' : Japanese OK?
\newbool{bxce@jaok}
%% switch 'bxce@ptex' : engine is pTeX-ish?
\newbool{bxce@ptex}
%% switch 'bxce@luatexja' : LuaTeX-ja is loaded?
\newbool{bxce@luatexja}
%% switch 'bxce@CJK' : CJK is loaded?
\newbool{bxce@CJK}
%% switch 'bxce@inputenc' : use inputenc?
\newbool{bxce@inputenc}
%% switch 'bxce@use@image' : use images?
\newbool{bxce@use@image}
%% switch 'bxce@use@bb' : use bb parameter?
\newbool{bxce@use@bb}

%% set \bxce@engine
\@onlypreamble\bxce@if@primitive
\def\bxce@if@primitive#1#2{%
  \edef\bxce@tmpa{\string#1}\edef\bxce@tmpb{\meaning#1}%
  \ifx\bxce@tmpa\bxce@tmpb #2\fi
}
\let\bxce@engine=d
\bxce@if@primitive\kanjiskip{\let\bxce@engine=p}
\bxce@if@primitive\enablecjktoken{\let\bxce@engine=u}
\bxce@if@primitive\XeTeXversion{\let\bxce@engine=x}
\bxce@if@primitive\luatexversion{\let\bxce@engine=l}

%% set switch 'bxce@ptex'
\bxce@ptexfalse
\if p\bxce@engine \bxce@ptextrue \fi
\if u\bxce@engine \bxce@ptextrue \fi
% NB. bxce@luatexja will be set at \bxce@resolve@image@metric

%% \bxce@check@inputenc
\let\bxce@check@inputenc\relax
\ifnum0\ifbxce@ptex1\fi\ifx d\bxce@engine1\fi >0
  \def\bxce@tmpa{utf8}%
  \ifx\inputencodingname\bxce@tmpa
    \bxce@inputenctrue
  \else\unless\ifx u\bxce@engine
    \def\bxce@check@inputenc{%
      \global\let\bxce@check@inputenc\relax
      \bxce@error
       {You must load 'inputenc' before this package\MessageBreak
        and use input encoding 'utf8'}\@ehc
    }%
  \fi\fi
\fi

%% \bxce@resolve@image@setup
\bxce@use@bbfalse
\bxce@use@imagetrue
\def\bxce@resolve@image@setup{%
  \global\let\bxce@resolve@image@setup\relax
  \ifx\bxce@family\relax % no-image
    \global\bxce@use@imagefalse
  \fi
  \ifbxce@use@image
    \@ifpackageloaded{graphicx}{}{%else
      \bxce@error
       {Package 'graphicx' is not loaded}\@ehc
      \global\bxce@use@imagefalse
    }%
  \fi
  \ifbxce@use@image
    \def\bxce@tmpa{dvipdfmx.def}%
    \ifx\Gin@driver\bxce@tmpa \global\bxce@use@bbtrue \fi
    \def\bxce@tmpa{dvipdfm.def}%
    \ifx\Gin@driver\bxce@tmpa \global\bxce@use@bbtrue \fi
  \fi
  \bxce@resolve@image@metric
  \global\let\bxce@resolve@image@metric\relax
}
\AtBeginDocument{\bxce@resolve@image@setup}
% auto-load graphicx if possible
\unless\ifx\bxce@family\relax
  \ifnum\ifdefined\pdfoutput \pdfoutput
        \else\ifdefined\outputmode \outputmode
        \else\ifx x\bxce@engine \@ne
        \else \z@ \fi\fi\fi >\z@
    \RequirePackage{graphicx}[]%
  \fi
\fi

%--------------------------------------- helpers

%% variables
\newcount\bxce@cnta
\let\bxce@arg\relax
\let\bxce@flag\@undefined

%% unique tokens
\def\bxce@end{\bxce@end@}
\def\bxce@mt{\bxce@mt@}
\let\bxce@mk\indent

%% \bxce@@BS
\begingroup \lccode`\*=`\\
\lowercase{\global\let\bxce@@BS=*}
\endgroup

%% \bxce@zspc : zenkaku space
\def\bxce@zspc{\kern1em\relax}

%% \bxce@nihil
\def\bxce@nihil{%
  \vrule\@width\z@\relax
}

%% \bxce@cond\ifXXX...\fi{<true>}{<false>}
\@gobbletwo\if\if \def\bxce@cond#1\fi{%
  #1\expandafter\@firstoftwo
  \else \expandafter\@secondoftwo \fi
}

%% \bxce@skip@space\CScont<space>
\def\bxce@skip@space#1 {#1}

%--------------------------------------- character database

%% character properties
\chardef\bxce@cp@base=1 % base char
\chardef\bxce@cp@comb=2 % combining char
\chardef\bxce@cp@mod=7 % modifier char
\chardef\bxce@cp@ris=3 % region indicator
\chardef\bxce@cp@evs=4 % emoji variant selector
\chardef\bxce@cp@zwj=5 % zero width joiner
\chardef\bxce@cp@tag=6 % tag char

%% constants
%[bxce@cp/<code>] : Unicode -> char property
%[bxce@ju/<jis>] : JIS-code -> Unicode (for jachar trick)

%% variables
\let\bxce@cp\relax

%% \bxce@@<codevalue>;
\let\bxce@@\relax % normally unexpandable

%% \bxce@set@cp<cp-value>{<range>,...}
\@onlypreamble\bxce@set@cp
\def\bxce@set@cp#1#2{%
  \let\bxce@cp=#1%
  \@for\bxce@arg:={#2}\do{%
    \expandafter\bxce@set@cp@a\bxce@arg--\bxce@end}%
}
\@onlypreamble\bxce@set@cp@a
\def\bxce@set@cp@a#1-#2-#3\bxce@end{%
  \ifx-#2-\bxce@set@cp@b{#1}{#1}%
  \else \bxce@set@cp@b{#1}{#2}%
  \fi
}
\@onlypreamble\bxce@set@cp@b
\def\bxce@set@cp@b#1#2{%
  \bxce@cnta="#1\relax
  \numdef\bxce@tmpb{"#2+1}%
  \@whilenum{\bxce@cnta<\bxce@tmpb}\do{%
    \edef\bxce@arg{{\the\bxce@cnta}{\hex{\bxce@cnta}}}%
    \expandafter\bxce@set@cp@c\bxce@arg
    \advance\bxce@cnta1 }%
}
\@onlypreamble\bxce@set@cp@c
\def\bxce@set@cp@c#1#2{%
  \cslet{bxce@cp/#1}\bxce@cp
  \bxce@set@cp@d{#1}{#2}%
}
\@onlypreamble\bxce@set@cp@d
\ifx u\bxce@engine
\def\bxce@set@cp@d#1#2{%
  \ifbxce@inputenc
    \ifnum#1>127 \ifnum\kcatcode#1=15
      \DeclareUnicodeCharacter{#2}{\bxce@@#2;}%
    \fi\fi
  \else
    \PackageWarningNoLine\bxce@pkgname
     {Some emoji characters might not be output.\MessageBreak
      You must load 'inputenc' before this package\MessageBreak
      and use input encoding 'utf8'}
    \let\bxce@set@cp@d\@gobbletwo
  \fi
}
\else
\def\bxce@set@cp@d#1#2{%
  \ifbxce@inputenc\ifnum#1>127
    \DeclareUnicodeCharacter{#2}{\bxce@@#2;}%
  \fi\fi
}
\fi

%%-------- data

%% make [bxce@cp] table
\bxce@set@cp\bxce@cp@base{%
  0023,002A,0030-0039,00A9,00AE,203C,2049,2122,2139,2194-2199,%
  21A9-21AA,231A-231B,2328,23CF,23E9-23EC,23ED-23EE,23EF,23F0,%
  23F1-23F2,23F3,23F8-23FA,24C2,25AA-25AB,25B6,25C0,25FB-25FE,%
  2600-2601,2602-2603,2604,260E,2611,2614-2615,2618,261D,2620,%
  2622-2623,2626,262A,262E,262F,2638-2639,263A,2640,2642,2648-2653,%
  265F,2660,2663,2665-2666,2668,267B,267E,267F,2692,2693,2694,2695,%
  2696-2697,2699,269B-269C,26A0-26A1,26A7,26AA-26AB,26B0-26B1,%
  26BD-26BE,26C4-26C5,26C8,26CE,26CF,26D1,26D3,26D4,26E9,26EA,%
  26F0-26F1,26F2-26F3,26F4,26F5,26F7-26F9,26FA,26FD,2702,2705,%
  2708-270C,270D,270F,2712,2714,2716,271D,2721,2728,2733-2734,2744,%
  2747,274C,274E,2753-2755,2757,2763,2764,2795-2797,27A1,27B0,27BF,%
  2934-2935,2B05-2B07,2B1B-2B1C,2B50,2B55,3030,303D,3297,3299,1F004,%
  1F0CF,1F170-1F171,1F17E-1F17F,1F18E,1F191-1F19A,1F1E6-1F1FF,%
  1F201-1F202,1F21A,1F22F,1F232-1F23A,1F250-1F251,1F300-1F30C,%
  1F30D-1F30E,1F30F,1F310,1F311,1F312,1F313-1F315,1F316-1F318,1F319,%
  1F31A,1F31B,1F31C,1F31D-1F31E,1F31F-1F320,1F321,1F324-1F32C,%
  1F32D-1F32F,1F330-1F331,1F332-1F333,1F334-1F335,1F336,1F337-1F34A,%
  1F34B,1F34C-1F34F,1F350,1F351-1F37B,1F37C,1F37D,1F37E-1F37F,%
  1F380-1F393,1F396-1F397,1F399-1F39B,1F39E-1F39F,1F3A0-1F3C4,1F3C5,%
  1F3C6,1F3C7,1F3C8,1F3C9,1F3CA,1F3CB-1F3CE,1F3CF-1F3D3,1F3D4-1F3DF,%
  1F3E0-1F3E3,1F3E4,1F3E5-1F3F0,1F3F3,1F3F4,1F3F5,1F3F7,1F3F8-1F407,%
  1F408,1F409-1F40B,1F40C-1F40E,1F40F-1F410,1F411-1F412,1F413,1F414,%
  1F415,1F416,1F417-1F429,1F42A,1F42B-1F43E,1F43F,1F440,1F441,%
  1F442-1F464,1F465,1F466-1F46B,1F46C-1F46D,1F46E-1F4AC,1F4AD,%
  1F4AE-1F4B5,1F4B6-1F4B7,1F4B8-1F4EB,1F4EC-1F4ED,1F4EE,1F4EF,%
  1F4F0-1F4F4,1F4F5,1F4F6-1F4F7,1F4F8,1F4F9-1F4FC,1F4FD,1F4FF-1F502,%
  1F503,1F504-1F507,1F508,1F509,1F50A-1F514,1F515,1F516-1F52B,%
  1F52C-1F52D,1F52E-1F53D,1F549-1F54A,1F54B-1F54E,1F550-1F55B,%
  1F55C-1F567,1F56F-1F570,1F573-1F579,1F57A,1F587,1F58A-1F58D,1F590,%
  1F595-1F596,1F5A4,1F5A5,1F5A8,1F5B1-1F5B2,1F5BC,1F5C2-1F5C4,%
  1F5D1-1F5D3,1F5DC-1F5DE,1F5E1,1F5E3,1F5E8,1F5EF,1F5F3,1F5FA,%
  1F5FB-1F5FF,1F600,1F601-1F606,1F607-1F608,1F609-1F60D,1F60E,1F60F,%
  1F610,1F611,1F612-1F614,1F615,1F616,1F617,1F618,1F619,1F61A,1F61B,%
  1F61C-1F61E,1F61F,1F620-1F625,1F626-1F627,1F628-1F62B,1F62C,1F62D,%
  1F62E-1F62F,1F630-1F633,1F634,1F635,1F636,1F637-1F640,1F641-1F644,%
  1F645-1F64F,1F680,1F681-1F682,1F683-1F685,1F686,1F687,1F688,1F689,%
  1F68A-1F68B,1F68C,1F68D,1F68E,1F68F,1F690,1F691-1F693,1F694,1F695,%
  1F696,1F697,1F698,1F699-1F69A,1F69B-1F6A1,1F6A2,1F6A3,1F6A4-1F6A5,%
  1F6A6,1F6A7-1F6AD,1F6AE-1F6B1,1F6B2,1F6B3-1F6B5,1F6B6,1F6B7-1F6B8,%
  1F6B9-1F6BE,1F6BF,1F6C0,1F6C1-1F6C5,1F6CB,1F6CC,1F6CD-1F6CF,1F6D0,%
  1F6D1-1F6D2,1F6D5,1F6D6-1F6D7,1F6E0-1F6E5,1F6E9,1F6EB-1F6EC,1F6F0,%
  1F6F3,1F6F4-1F6F6,1F6F7-1F6F8,1F6F9,1F6FA,1F6FB-1F6FC,1F7E0-1F7EB,%
  1F90C,1F90D-1F90F,1F910-1F918,1F919-1F91E,1F91F,1F920-1F927,%
  1F928-1F92F,1F930,1F931-1F932,1F933-1F93A,1F93C-1F93E,1F93F,%
  1F940-1F945,1F947-1F94B,1F94C,1F94D-1F94F,1F950-1F95E,1F95F-1F96B,%
  1F96C-1F970,1F971,1F972,1F973-1F976,1F977-1F978,1F97A,1F97B,%
  1F97C-1F97F,1F980-1F984,1F985-1F991,1F992-1F997,1F998-1F9A2,%
  1F9A3-1F9A4,1F9A5-1F9AA,1F9AB-1F9AD,1F9AE-1F9AF,1F9B0-1F9B9,%
  1F9BA-1F9BF,1F9C0,1F9C1-1F9C2,1F9C3-1F9CA,1F9CB,1F9CD-1F9CF,%
  1F9D0-1F9E6,1F9E7-1F9FF,1FA70-1FA73,1FA74,1FA78-1FA7A,1FA80-1FA82,%
  1FA83-1FA86,1FA90-1FA95,1FA96-1FAA8,1FAB0-1FAB6,1FAC0-1FAC2,%
  1FAD0-1FAD6}
\bxce@set@cp\bxce@cp@comb{%
  20E0,20E3}
\bxce@set@cp\bxce@cp@mod{%
  1F3FB-1F3FF}
\bxce@set@cp\bxce@cp@ris{%
  1F1E6-1F1FF}
\bxce@set@cp\bxce@cp@evs{%
  FE0E-FE0F}
\bxce@set@cp\bxce@cp@zwj{%
  200D}
\bxce@set@cp\bxce@cp@tag{%
  E0020-E007F}

%% make [bxce@ju] table
\ifx p\bxce@engine
  \def\bxce@do#1#2{\csedef{bxce@ju/\jis"#1}{\number"#2}}
  \bxce@do{2169}{2642}
  \bxce@do{216A}{2640}
\fi

%--------------------------------------- user interface

%%<*> \coloremoji*[<option>,...]{<text>}
\newrobustcmd*{\coloremoji}{%
  \bxce@enghost\bxce@coloremoji@a
}
\def\bxce@coloremoji@a{%
  \begingroup
    \@ifnextchar[%]
      {\bxce@coloremoji@b}%
      {\bxce@coloremoji@b[]}%
}
\def\bxce@coloremoji@b[#1]#2{%
    \setkeys{bxce}{#1}%
    \bxce@parse@text{#2}%
    \bxce@output
  \endgroup
  \bxce@postghost
}

%%<*> \coloremojiucs*[<option>,...]{<codevalue> ...}
\newrobustcmd*{\coloremojiucs}{%
  \bxce@enghost\bxce@coloremojiucs@a
}
\def\bxce@coloremojiucs@a{%
  \begingroup
    \@ifnextchar[%]
      {\bxce@coloremojiucs@b}%
      {\bxce@coloremojiucs@b[]}%
}
\def\bxce@coloremojiucs@b[#1]#2{%
    \setkeys{bxce}{#1}%
    \bxce@parse@text@ccseq{#2}%
    \bxce@output
  \endgroup
  \bxce@postghost
}

%%<*> \coloremojicode*[<option>,...]{<codevalue> ...}
% An alias to \coloremojiucs.
\newcommand*{\coloremojicode}{\coloremojiucs}

%--------------------------------------- parse text

%% variables
\newbool{bxce@escape}
\let\bxce@text\relax
\let\bxce@cseq\@empty

%% error messages
\def\bxce@err@intcc#1#2{%
  \bxce@error{INTERNAL ERROR (#1)\MessageBreak
      (code = U+#2)}\@ehc
}
\def\bxce@warn@locmb#1{%
  \bxce@warn{Lone combining character found\MessageBreak
      (code = U+#1)}%
}
\def\bxce@warn@lotag#1{%
  \bxce@warn{Lone tag character found\MessageBreak
      (code = U+#1)}%
}
\def\bxce@warn@loris#1{%
  \bxce@warn{Unpaired region indicator symbol found\MessageBreak
      (code = U+#1)}%
}
\def\bxce@warn@lschr{%
  \bxce@warn{Some characters given to \string\coloremoji\space
    are\MessageBreak unimplemented and thus lost,}%
}

%% \bxce@expand@text{<text>}
\def\bxce@expand@text#1{%
  \bxce@check@inputenc
  \begingroup
    \ifbxce@CJK \bxce@CJK@preproc \fi
    % Here protected expansion is needed, but still active UTF-8 byte
    % sequences need to be expanded.
    \ifdefined\UTFviii@two@octets@combine \bxce@newltx@patch \fi
    \let\PackageError\bxce@PackageError
    \def\bxce@@{\"}\let\IeC\@firstofone \let\"\relax
    \undef\bxce@flag
    \protected@xdef\@gtempa{#1}%
    \ifx\bxce@flag\relax
      \bxce@warn@lschr
    \fi
  \endgroup
  \let\bxce@text\@gtempa
}
\def\bxce@PackageError#1#2#3{%
  \expandafter\@gobble\csname bxce@flag\endcsname}
\def\bxce@newltx@patch{%
  \let\UTF@two@octets@noexpand\@empty
  \let\UTF@three@octets@noexpand\@empty
  \let\UTF@four@octets@noexpand\@empty
}

%% \bxce@parse@text{<text>}
\def\bxce@parse@text#1{%
  \bxce@expand@text{#1}%
  \edef\bxce@text{\detokenize\expandafter{\bxce@text}}%
  \expandafter\bxce@parse@first\bxce@text\bxce@end
\bxDebug{first=\meaning\bxce@text}%
  \expandafter\bxce@parse@second\bxce@text\bxce@end
\bxDebug{second=\meaning\bxce@text}%
  \expandafter\bxce@parse@third\bxce@text\bxce@end
\bxDebug{third=\meaning\bxce@text}%
}
% first stage
\let\bxce@T\relax
\let\bxce@O\relax
\let\bxce@S\relax
\let\bxce@E\relax
\let\bxce@J\relax
\let\bxce@c\relax
\def\bxce@parse@first{%
  \let\bxce@text\@empty
  \bxce@escapefalse
  \bxce@parse@first@a
}
\def\bxce@parse@first@a{%
  \futurelet\bxce@arg\bxce@parse@first@b
}
\def\bxce@parse@first@b{%
  \bxce@cond\ifx\bxce@arg\bxce@end\fi{%
    \appto\bxce@text{\bxce@T.}%
    \@gobble
  }{\bxce@cond\ifx\bxce@arg\@sptoken\fi{%
    \bxce@escapefalse
    \appto\bxce@text{\bxce@S.}%
    \bxce@skip@space\bxce@parse@first@a
  }{\bxce@cond\ifbxce@escape\fi{%
    \bxce@parse@first@c
  }{%else
    \bxce@parse@first@d
  }}}%
}
\def\bxce@parse@first@c#1{%
  \bxce@escapefalse
  \bxce@cond\if#1"\fi{%
    \bxce@parse@first@f
  }{%else
    \bxce@parse@first@e{#1}%
    \bxce@parse@first@a
  }%
}
\def\bxce@parse@first@d#1{%
  \bxce@cond\if#1\bxce@@BS\fi{%
    \bxce@escapetrue
  }{%else
    \bxce@parse@first@e{#1}%
  }%
  \bxce@parse@first@a
}
\def\bxce@parse@first@e#1{%
  \bxce@cnta=`#1\relax
  \bxce@parse@first@trick#1%
  \letcs\bxce@tmpa{bxce@cp/\the\bxce@cnta}%
  \ifdefined\bxce@tmpa
    \eappto\bxce@text{\bxce@E\the\bxce@tmpa{\hex{\bxce@cnta}}}%
  \else
    \appto\bxce@text{\bxce@O#1}%
  \fi
}
\def\bxce@parse@first@f#1;{%
  \bxce@cnta="#1\relax
  \letcs\bxce@tmpa{bxce@cp/\the\bxce@cnta}%
  \unless\ifdefined\bxce@tmpa
    \chardef\bxce@tmpa\z@
    \bxce@err@intcc{1}{#1}%
  \fi
  \eappto\bxce@text{\bxce@E\the\bxce@tmpa{#1}}%
  \bxce@parse@first@a
}
% second stage
\def\bxce@parse@second{%
  \let\bxce@text\@empty
  \let\bxce@T\bxce@parse@second@T
  \let\bxce@O\bxce@parse@second@O
  \let\bxce@S\bxce@parse@second@S
  \let\bxce@E\bxce@parse@second@E
}
\protected\def\bxce@parse@second@T.\bxce@end{%
  \appto\bxce@text{\bxce@T}%
}
\protected\def\bxce@parse@second@O#1{%
  \appto\bxce@text{\bxce@O{#1}}%
}
\protected\def\bxce@parse@second@S#1{%
  \appto\bxce@text{\bxce@O{ }}%
}
\protected\def\bxce@parse@second@E#1#2#3#4{%
  % check if look-ahead is EVS
  \ifnum0\ifx#3\bxce@E \ifnum#4=\bxce@cp@evs 1\fi\fi=1
    \expandafter\bxce@parse@second@E@a
  \else \expandafter\bxce@parse@second@E@b
  \fi #1{#2}#3#4%
}
\def\bxce@parse@second@E@a#1#2#3#4#5{%
  % purge the EVS entry and go back
  \bxce@parse@second@E#1{#2}%
}
\def\bxce@parse@second@E@b#1#2#3#4{%
  % current character: #1=cp, #2=code
  % look-ahead character: #3=type, #4=cp
  \ifnum#1=\bxce@cp@base
    \ifnum0\ifx#3\bxce@E \ifnum#4=\bxce@cp@comb 1%
                         \else\ifnum#4=\bxce@cp@mod 1%
                         \else\ifnum#4=\bxce@cp@tag 1\fi\fi\fi\fi=1
      % head of modifier/combining sequence
      \def\bxce@cseq{#1{#2}}%
    \else
      % simple base character
      \appto\bxce@text{\bxce@E{#1{#2}}}%
    \fi
  \else\ifnum#1=\bxce@cp@comb
    \ifx\bxce@cseq\@empty
      % headless combining character, fallback
      \bxce@warn@locmb{#2}%
      \appto\bxce@text{\bxce@O{#2}}%
    \else\ifnum0\ifx#3\bxce@E \ifnum#4=\bxce@cp@comb 1\fi\fi=1
      % midst of combining sequence
      \appto\bxce@cseq{#1{#2}}%
    \else
      % tail of combining sequence
      \eappto\bxce@text{\bxce@E{\bxce@cseq#1{#2}}}%
      \let\bxce@cseq\@empty
    \fi\fi
  \else\ifnum#1=\bxce@cp@mod
    \ifx\bxce@cseq\@empty
      % baseless modifier character is a loner
      \eappto\bxce@text{\bxce@E{\the\bxce@cp@base{#2}}}%
    \else\ifnum0\ifx#3\bxce@E \ifnum#4=\bxce@cp@comb 1\fi\fi=1
      % midst of combining sequence
      \appto\bxce@cseq{#1{#2}}%
    \else
      % tail of modifier sequence
      \eappto\bxce@text{\bxce@E{\bxce@cseq#1{#2}}}%
      \let\bxce@cseq\@empty
    \fi\fi
  \else\ifnum#1=\bxce@cp@ris
    \ifx\bxce@cseq\@empty
      \ifnum0\ifx#3\bxce@E \ifnum#4=\bxce@cp@ris 1\fi\fi=1
        % nead of region indicator sequence
        \def\bxce@cseq{#1{#2}}%
      \else
        % lone region indicator characer, fallback
        \bxce@warn@loris{#2}%
        \eappto\bxce@text{\bxce@E{\the\bxce@cp@base{#2}}}%
      \fi
    \else
      % tail of region indicator sequence
      \eappto\bxce@text{\bxce@E{\bxce@cseq#1{#2}}}%
      \let\bxce@cseq\@empty
    \fi
  \else\ifnum#1=\bxce@cp@tag
    \ifx\bxce@cseq\@empty
      % headless tag character, fallback
      \bxce@warn@lotag{#2}%
      \appto\bxce@text{\bxce@O{#2}}%
    \else\ifnum0\ifx#3\bxce@E \ifnum#4=\bxce@cp@tag 1\fi\fi=1
      % midst of emoji tag sequence
      \appto\bxce@cseq{#1{#2}}%
    \else
      % tail of emoji tag sequence
      \eappto\bxce@text{\bxce@E{\bxce@cseq#1{#2}}}%
      \let\bxce@cseq\@empty
    \fi\fi
  \else\ifnum#1=\bxce@cp@evs
    % currently EVS is totally ignored
  \else\ifnum#1=\bxce@cp@zwj
    \appto\bxce@text{\bxce@J}%
  \fi\fi\fi\fi\fi\fi\fi
  #3#4% NB #4 is unbraced
}
% third stage
\def\bxce@parse@third{%
  \let\bxce@text\@empty
  \let\bxce@cseq\@empty
  \let\bxce@T\bxce@parse@third@T
  \let\bxce@O\bxce@parse@third@O
  \let\bxce@E\bxce@parse@third@E
  \let\bxce@J\bxce@parse@third@J
}
\protected\def\bxce@parse@third@T\bxce@end{}
\protected\def\bxce@parse@third@O#1{%
  \appto\bxce@text{\bxce@wr{#1}}%
}
\protected\def\bxce@parse@third@E#1#2{%
  \appto\bxce@cseq{#1}%
  \unless\ifx#2\bxce@J
    \bxce@parse@third@out
    \let\bxce@cseq\@empty
  \fi
  #2%
}
\protected\def\bxce@parse@third@J#1{%
  \ifx\bxce@cseq\@empty
    \appto\bxce@text{\bxce@put{200D}}%
  \else\unless\ifx#1\bxce@E
    \bxce@parse@third@out
    \let\bxce@cseq\@empty
    \appto\bxce@text{\bxce@put{200D}}%
  \else
    \eappto\bxce@cseq{\the\bxce@cp@zwj{200D}}%
  \fi\fi
  #1%
}
\def\bxce@parse@third@out{%
  \let\bxce@tmpa\@empty
  \def\bxce@tmpb{\bxce@put@cseq}%
  \expandafter\bxce@parse@third@out@a\bxce@cseq..%
}
\def\bxce@parse@third@out@a#1#2{%
  \bxce@cond\ifx.#1\fi{%
    \eappto\bxce@text{\bxce@tmpb{\bxce@tmpa}}%
  }{%else
    \ifnum#1=\bxce@cp@tag
      \def\bxce@tmpb{\bxce@put@cseqtag}%
    \fi
    \appto\bxce@tmpa{\bxce@c{#2}}%
    \bxce@parse@third@out@a
  }%
}

%% \bxce@parse@text@ccseq
\def\bxce@parse@text@ccseq#1{%
  \bxce@parse@first@ccseq{#1}%
\bxDebug{first=\meaning\bxce@text}%
  \expandafter\bxce@parse@second\bxce@text\bxce@end
\bxDebug{second=\meaning\bxce@text}%
  \expandafter\bxce@parse@third\bxce@text\bxce@end
\bxDebug{third=\meaning\bxce@text}%
}
\def\bxce@parse@first@ccseq#1{%
  \edef\bxce@tmpa{#1}%
  \edef\bxce@tmpa{{\detokenize\expandafter{\bxce@tmpa}}}%
  \expandafter\bxce@parse@ccseq\bxce@tmpa\bxce@parse@first@ccseq@a
}
\def\bxce@parse@first@ccseq@a#1{%
  \let\bxce@do\bxce@parse@first@ccseq@do
  \let\bxce@bad\bxce@warn@ivcod
  \let\bxce@text\@empty
  #1%
  \appto\bxce@text{\bxce@T.}%
}

%% \bxce@parse@first@ccseq@do{<code-value>}
\def\bxce@parse@first@ccseq@do#1{%
  \bxce@cnta"0#1\relax
  \letcs\bxce@tmpa{bxce@cp/\the\bxce@cnta}%
  \ifdefined\bxce@tmpa
    \eappto\bxce@text{\bxce@E\the\bxce@tmpa{#1}}%
  \else
    \bxce@char@token\bxce@tmpa\bxce@cnta
    \eappto\bxce@text{\bxce@O\bxce@tmpa}%
  \fi
}

%% \bxce@parse@first@trick<char>
% The engine-specific "trick".
\ifx p\bxce@engine % pTeX
  %% trick to hedge ja-chars
  \def\bxce@parse@first@trick#1{%
    \ifnum\bxce@cnta>\@cclv % ja-char
      \bxce@cnta0\@nameuse{bxce@ju/\the\bxce@cnta}\relax
    \fi
  }
\else\ifx x\bxce@engine % XeTeX
  %% trick to avoid anomaly of old XeTeX
  \def\bxce@parse@first@trick#1{%
    \@tempcnta\bxce@cnta \divide\@tempcnta"800
    \ifnum\@tempcnta=27 % surrogate value
      \bxce@parse@first@surrogate
    \fi
  }
  \def\bxce@parse@first@surrogate#1\bxce@parse@first@a#2{%
    \bxce@cnta=\numexpr\bxce@cnta*"400+`#2-"35FDC00\relax
    #1\bxce@parse@first@a % continue!
  }
\else
  \let\bxce@parse@first@trick\@gobble
\fi\fi

%--------------------------------------- emoji name database

%% variables
\let\bxce@ndindex\relax
\let\bxce@ndmaxindex\relax
%[bxce@nd/<name>] : name -> ccseq
%[bxce@ndi/<char>] : first char -> sector
%[bxce@ndrs/<sector>] : the sector is already read?

%% \bxce@read@name@data@file{<sector>}
\def\bxce@read@name@data@file#1{%
  \begingroup
    \endlinechar\m@ne
    \catcode13=9 \catcode32=10 \catcode64=11
    \catcode45=12 \catcode46=12 \catcode47=12
    \ifnum#1<\z@ % initizlize
      \let\bxce@ndindex\m@ne
      \let\bxce@do\bxce@read@name@data@file@init@do
    \else % read a sector
      \chardef\bxce@ndindex=#1\relax
      \PackageInfo\bxce@pkgname
       {Loading emoji name data file\MessageBreak
        (sector \the\bxce@ndindex)}%
      \global\cslet{bxce@ndrs/\the\bxce@ndindex}{t}%
      \let\bxce@do\bxce@read@name@data@file@sec@do
    \fi
    \input{bxcoloremoji-names.def}%
  \endgroup
}
\def\bxce@read@name@data@file@init@do#1#2{%
  %\bxDebug{nd-init:#1=#2}%
  \csgdef{bxce@ndi/#1}{#2}%
}
\def\bxce@read@name@data@file@sec@do#1#2{%
  %\bxDebug{nd-sec-\the\bxce@ndindex:#1=#2}%
  \csgdef{bxce@nd/#1}{#2}%
}

%% initizlize
\bxce@read@name@data@file{-1}
\mathchardef\bxce@ndmaxindex=\csuse{bxce@ndi/z}\relax

%% additional definitions
\csgdef{bxce@nd/+}{200D}
\csgdef{bxce@nd/@}{E007F}
\csgdef{bxce@nd/!<}{2B05}
\csgdef{bxce@nd/!>}{27A1}
\csgdef{bxce@nd/!male}{2642}
\csgdef{bxce@nd/!female}{2640}
\csgdef{bxce@nd/!flag}{1F3F4}
\csgdef{bxce@nd/!black}{2B1B}
\csgdef{bxce@nd/!white}{2B1C}
\csgdef{bxce@nd/!red}{1F7E5}
\csgdef{bxce@nd/!blue}{1F7E6}
\csgdef{bxce@nd/!orange}{1F7E7}
\csgdef{bxce@nd/!yellow}{1F7E8}
\csgdef{bxce@nd/!green}{1F7E9}
\csgdef{bxce@nd/!purple}{1F7EA}
\csgdef{bxce@nd/!brown}{1F7EB}
\AtEndOfPackage{%
  \begingroup
    \def\bxce@do#1"#2\relax{\def\bxce@tmpa{#2}}%
    \bxce@cnta="20
    \@whilenum{\bxce@cnta<"7F}\do{%
      % register '@!'--'@~' (U+E0021--E007E)
      \chardef\bxce@tmpa\bxce@cnta
      \expandafter\bxce@do\meaning\bxce@tmpa\relax
      \bxce@char@token\bxce@tmpb\bxce@cnta
      \csxdef{bxce@nd/@\bxce@tmpb}{E00\bxce@tmpa}%
      \ifnum\bxce@cnta>"40 \ifnum\bxce@cnta<"5B
        % register '!A'--'!Z' (U+1F1E6--1F1FF)
        \chardef\bxce@tmpa\numexpr\bxce@cnta+"A5\relax
        \expandafter\bxce@do\meaning\bxce@tmpa\relax
        \csxdef{bxce@nd/!\bxce@tmpb}{1F1\bxce@tmpa}%
      \fi\fi
      \advance\bxce@cnta\@ne
    }%
  \endgroup
}

%% \bxce@load@sname@definition{<sname>}
\def\bxce@load@sname@definition#1{%
  \bxce@load@sname@definition@a#1\bxce@end
}
\def\bxce@load@sname@definition@a#1#2\bxce@end{%
  \letcs\bxce@tmpa{bxce@ndi/#1}%
  \ifdefined\bxce@tmpa
    \ifcsdef{bxce@ndrs/\number\bxce@tmpa}{}{%else
      \bxce@read@name@data@file{\bxce@tmpa}%
    }%
  \fi
}

%%<+> \coloremojiLoadNames{<sname> ...}
\newrobustcmd*\coloremojiLoadNames[1]{%
  \edef\bxce@tmpa{#1}%
  \edef\bxce@tmpa{\detokenize\expandafter{\bxce@tmpa} @ }%
  \expandafter\bxce@load@names@a\bxce@tmpa
}
\def\bxce@load@names@a#1 {%
  \bxce@cond\ifx @#1\fi{}{%else
    \bxce@load@names@b#1??\bxce@end
    \bxce@load@names@a
  }%
}
\def\bxce@load@names@b#1#2\bxce@end{%
  \ifx:#1%
    \bxce@load@sname@definition{#2}%
  \else
    \bxce@load@sname@definition{#1#2}%
  \fi
}

%%<+> \coloremojiLoadAllNames
\newrobustcmd*\coloremojiLoadAllNames{%
  \global\let\coloremojiLoadAllNames\relax
  \global\let\bxce@lookup@sname\bxce@parse@sname
  \bxce@cnta\m@ne
  \@whilenum{\bxce@cnta<\bxce@ndmaxindex}\do{%
    \advance\bxce@cnta\@ne
    \ifcsdef{bxce@ndrs/\number\bxce@cnta}{}{%else
      \bxce@read@name@data@file{\bxce@cnta}%
    }%
  }%
}

%--------------------------------------- parse character code

% A character code is either of the following:
% - a hex string of the Unicode value (case-insensitive);
% - a short name input ':<short-name>:' (in lowercase);
%   here ':' of the either side can be omitted as long as the input
%   is unambiguous (not regarded as code value).

%% error message
\def\bxce@warn@ivcod#1{%
  \bxce@warn{Invalid character code found\MessageBreak
    (#1)}%
}

%% \bxce@parse@ccseq{<cc> ...}\CScont
% Parses a character code sequence and returns the
% \bxce@do-list of the code values; i.e. it expans to
% \CScont{\bxce@do{<cv>}...}.
% Unknown names are converted to \bxce@bad{<name>}.
\def\bxce@parse@ccseq#1{%{<in>}|\CScont
  \bxce@parse@ccseq@a#1 @ @%
}
% (\bxce@parse@ccseq@a<in> @ <out>@\CScont)
\def\bxce@parse@ccseq@a#1 {%<cc> |...
  \bxce@cond\ifx @#1\fi{%
    \bxce@parse@ccseq@e
  }{\bxce@cond\ifx\bxce@mt#1\bxce@mt\fi{%empty-string
    \bxce@parse@ccseq@a
  }{%else
    \bxce@parse@cc{#1}\bxce@parse@ccseq@b\bxce@parse@ccseq@c
        \bxce@parse@ccseq@d{#1}%
  }}%
}
\def\bxce@parse@ccseq@b#1#2#3@#4@{%{<hex>}{<cc>}<in> @ <out>@|...
  \bxce@parse@ccseq@a#3@#4\bxce@do{#1}@%
}
\def\bxce@parse@ccseq@c#1#2{%{<seq>}{<cc>}|<in> @...
  \bxce@parse@ccseq@a#1 %
}
\def\bxce@parse@ccseq@d#1#2@#3@{%{<cc>}<in> @ <out>@|...
  \bxce@parse@ccseq@a#2@#3\bxce@bad{#1}@%
}
\def\bxce@parse@ccseq@e#1@#2{%<out>@\CScont
  #2{#1}%
}

%% \bxce@parse@cc{<cc>}\CSok\CSrs\CSerr
% Parses the character name or code value, and expands to:
% \CSok{<code-value>} if <cc> is a valid hex string;
% \CSrs{<out-cc> ...} if <Cc> is a short name; or
% \CSerr otherwise.
\def\bxce@parse@cc#1{%{<cc>}|...
  \bxce@parse@hex{#1}\bxce@parse@cc@a\bxce@parse@cc@b{#1}%
}
\def\bxce@parse@cc@a#1#2#3#4#5{%{<hex>}{<cc>}\CSok\CSrs\CSerr
  #3{#1}%
}
\def\bxce@parse@cc@b#1#2{%{<cc>}\CSok|\CSrs\CSerr
  \bxce@trim@colons{#1}\bxce@lookup@sname
}

%% \bxce@trim@colons{<word>}\CScont
\def\bxce@trim@colons#1{%{<word>}|\CScont
  \bxce@trim@colons@a#1@:@\bxce@mk
}
\def\bxce@trim@colons@a#1#2:@#3\bxce@mk{%
  \bxce@cond\if:#1\fi{%
    \bxce@trim@colons@b#2@\bxce@mk
  }{%else
    \bxce@trim@colons@b#1#2@\bxce@mk
  }%
}
\def\bxce@trim@colons@b#1@#2\bxce@mk#3{%
  #3{#1}%
}

%% \bxce@lookup@sname{<sname>}\CSok\CSerr
\def\bxce@lookup@sname#1{%{<sname>}|\CSok\CSerr
  \ifcsdef{bxce@nd/#1}{%
    \bxce@parse@sname@out{#1}%
  }{%else
    \bxce@load@sname@definition{#1}%
    \bxce@parse@sname{#1}%
  }%
}

%% \bxce@parse@sname{<sname>}\CSok\CSerr
\def\bxce@parse@sname#1{%{<sname>}|\CSok\CSerr
  \ifcsdef{bxce@nd/#1}{%
    \bxce@parse@sname@out{#1}%
  }{%else
    \@secondoftwo
  }%
}
\def\bxce@parse@sname@out#1{%{<sname>}|\CSok\CSerr
  \expandafter\expandafter\expandafter
      \bxce@parse@sname@out@a\csname bxce@nd/#1\endcsname\bxce@end
}
\def\bxce@parse@sname@out@a#1\bxce@end#2#3{%<res>\end\CSok\CSerr
  #2{#1}%
}

%--------------------------------------- image output

%% variables
\newbool{bxce@ctdir}
\newdimen\bxce@cwd
\newdimen\bxce@disp@dim
\newskip\bxce@inter@skip

%% \bxce@image@path{<name>}
\def\bxce@image@path#1{%
  \coloremojidir\bxce@family/#1.\bxce@ext
}

%% \bxce@unit / \bxce@disp / \bxce@inter
\def\bxce@unit{em}
\def\bxce@disp{0.12}
\def\bxce@inter{0pt plus 0.1em}

%% \bxce@output
\def\bxce@output{%
  \ifmmode \nfss@text{\bxce@output@a}%
  \else \bxce@output@a
  \fi
}
\def\bxce@output@a{%
  \bxce@resolve@image@setup
  \bxce@iftdir{\bxce@ctdirtrue}{\bxce@ctdirfalse}%
  \bxce@cwd=\bxce@scale\bxce@unit\relax
  \bxce@disp@dim=\bxce@disp\bxce@unit\relax
  \bxce@inter@skip=\bxce@inter\relax
  \bxce@text
  \unskip
}

%% \bxce@wr{<char>}
\def\bxce@wr#1{%
  #1\hskip\bxce@inter@skip
}

%% \bxce@put{<code1>}
\def\bxce@put#1{%
  \bxce@if@image@exists{#1}{%
    \bxce@put@image{#1}%
  }{%else
    \bxce@char@token\bxce@arg{"#1}%
    \bxce@arg \hskip\bxce@inter@skip
  }%
}

%% \bxce@put@cseq{\bxce@c<cp>{<code>}...}
\def\bxce@c@hyph#1{-#1}
\def\bxce@c@first#1#2{#2}
\protected\def\bxce@put@cseq#1{%
  \let\bxce@c\bxce@c@hyph
  \edef\bxce@cseq{\bxce@c@first#1}%
\bxDebug{cseq:\bxce@cseq}%
  \bxce@if@image@exists{\bxce@cseq}{%
    \bxce@put@image{\bxce@cseq}%
  }{%else
    \let\bxce@c\bxce@put@cseq@a#1%
  }%
}
\def\bxce@@ZWJ@code{200D}
\def\bxce@put@cseq@a#1{%
  \def\bxce@tmpa{#1}\ifx\bxce@tmpa\bxce@@ZWJ@code
    \bxce@depict@fallback@ZWJ
  \else \bxce@put{#1}%
  \fi
}

%% \bxce@put@cseqtag{\bxce@c<cp>{<code>}...}
\protected\def\bxce@put@cseqtag#1{%
  \let\bxce@c\bxce@c@hyph
  \edef\bxce@cseq{\bxce@c@first#1}%
\bxDebug{cseqtag:\bxce@cseq}%
  \bxce@if@image@exists{\bxce@cseq}{%
    \bxce@put@image{\bxce@cseq}%
  }{%else
    \bxce@put@cseqtag@a#1\bxce@end
  }%
}
\def\bxce@put@cseqtag@a#1#2#3\bxce@end{%
  \bxce@put{#2}%
  \bxce@char@token\bxce@arg\bxce@repch
  \bxce@arg \hskip\bxce@inter@skip
}

%% \bxce@if@image@exists{<name>}
\def\bxce@if@image@exists#1{%
  \bxce@cond\ifbxce@use@image\fi{%
    \IfFileExists{\bxce@image@path{#1}}%
  }{%else
    \@secondoftwo % always false
  }%
}

%% \bxce@put@image
\def\bxce@put@image#1{%
  \edef\bxce@tmpa{%
    \lower\bxce@disp@dim\hbox{%
      \ifbxce@ctdir\noexpand\rotatebox[origin=c]{90}\fi{%
      \noexpand\includegraphics
      [width=\bxce@cwd \ifbxce@use@bb ,bb=\bxce@bb \fi]%
      {\bxce@image@path{#1}}}}}%
  \bxce@tmpa \hskip\bxce@inter@skip
}

%--------------------------------------- fallback ZWJ

%%<+> \coloremojiZWJSymbolColor
\newcommand\coloremojiZWJSymbolColor{green}

\def\bxce@depict@fallback@ZWJ{%
  \begingroup
    \bxce@set@ZWJ@color
    \bxce@ZWJ@sym
  \endgroup
}

%% \bxce@ZWJ@sym
\def\bxce@ZWJ@sym{%
  \lower\dimexpr\bxce@disp\bxce@unit+0.2\bxce@unit\hb@xt@\z@{%
    \kern-0.2\bxce@unit\relax
    \vrule\@width0.1\bxce@unit\@height0.2\bxce@unit\relax
    \vrule\@width0.2\bxce@unit\@height0.1\bxce@unit\relax
    \vrule\@width0.1\bxce@unit\@height0.2\bxce@unit\relax
    \hss}%
}

%% \bxce@set@ZWJ@color
\def\bxce@set@ZWJ@color{%
  \ifdefined\color
    \edef\bxce@arg{{\coloremojiZWJSymbolColor}}%
    \expandafter\color\bxce@arg
  \fi
}

%--------------------------------------- tate direction

%% constants
\chardef\bxce@luatexja@tate=3

%% \bxce@iftdir{<tate>}{<yoko>}
\let\bxce@iftdir\@secondoftwo % always yoko

%% \bxce@resolve@image@metric
\def\bxce@resolve@image@metric{%
  % set 'bxce@luatexja'
  \if l\bxce@engine
    \@ifpackageloaded{luatexja}{%
      \bxce@luatexjatrue
    }{}%
  \fi
  %
  \ifbxce@luatexja
    \global\bxce@jaoktrue
    \global\chardef\bxce@zspc="3000
    \ifdefined\ltj@curtfnt
      \gdef\bxce@iftdir{\bxce@cond
        \ifnum\ltjgetparameter{direction}=\bxce@luatexja@tate\fi}%
    \fi
    \gdef\bxce@unit{\zw}%
    \gdef\bxce@disp{\bxce@iftdir{0.5}{0.12}}%
    \gdef\bxce@inter{%
      \expandafter\bxce@inter@a\number\ltjgetparameter{kanjiskip}\relax}%
    \gdef\bxce@inter@a##1\relax{%
      \ifdim\glueexpr##1=\maxdimen \z@skip \else ##1\fi}%
  \else\ifbxce@ptex
    \global\bxce@jaoktrue
    \global\chardef\bxce@zspc=\jis"2121\relax
    \gdef\bxce@iftdir{%
      \ifbool{tdir}{\notbool{mdir}}{\@secondoftwo}}%
    \gdef\bxce@unit{zw}%
    \gdef\bxce@disp{\bxce@iftdir{0.5}{0.12}}%
    \gdef\bxce@inter{\kanjiskip}%
  \fi\fi
  \if d\bxce@engine
    \@ifpackageloaded{CJK}{%
      \unless\ifdefined\CJKhook \let\CJKhook\@empty \fi
      \gappto\CJKhook{\bxce@CJKtrue}%
    }{}%
  \fi
}

%% try once now
\bxce@resolve@image@metric

%--------------------------------------- character token

%% \bxce@geta : geta mark
%% \bxce@maxcc : max 'safe' codevalue
%% \bxce@repch : replacement char
\ifx d\bxce@engine
\chardef\bxce@geta=`\=\relax
\chardef\bxce@maxcc="FF
\chardef\bxce@repch=\bxce@geta
\else\ifx p\bxce@engine
\chardef\bxce@geta=\jis"222E\relax
\chardef\bxce@maxcc="7F
\chardef\bxce@repch=\bxce@geta
\else
\chardef\bxce@geta="3013\relax
\chardef\bxce@maxcc="10FFFF
\chardef\bxce@repch="FFFD
\fi\fi

%% \bxce@char@token\CS{<codvalue>}
\def\bxce@char@token#1#2{%
  \begingroup
    \bxce@cnta=#2\relax
    \ifnum\bxce@cnta<0 \bxce@cnta=\bxce@geta \fi
    \ifnum\bxce@cnta>\bxce@maxcc \bxce@cnta=\bxce@geta \fi
    \ifnum0\ifbxce@ptex\ifnum\bxce@cnta>"7F 1\fi\fi=1
      \kansujichar1=\bxce@cnta
      \xdef\@gtempa{\kansuji1}%
    \else
      \lccode`\*=\bxce@cnta
      \lowercase{\xdef\@gtempa{*}}%
    \fi
  \endgroup
  \let#1=\@gtempa
}

%--------------------------------------- ghost something

%% \bxce@postghost
\let\bxce@postghost\relax

%% \bxce@enghost\CScont...
\def\bxce@enghost#1#2#{% brace delimit
  \bxce@enghost@a{#2}%
  \@ifstar{#1}{#1}#2%
}
\def\bxce@enghost@a#1{%
% NB. It could be called before \bxce@resolve@image@metric.
% Then ghost setting could be inappropriate.
  \bxce@cond\ifbxce@jaok\fi{%
    \bxce@cond\ifmmode\fi{%
      \let\bxce@postghost\relax
    }{%else
      \bxce@with@star{#1}{%
        \bxce@nihil
        \let\bxce@postghost\bxce@nihil
      }{%else
        \bxce@zspc \kern-1\bxce@unit\relax
        \def\bxce@postghost{%
          \kern-1\bxce@unit\relax \bxce@zspc}%
      }%
    }%
  }{%else
    \leavevmode
    \let\bxce@postghost\relax
  }%
}

%% \bxce@with@star{<str>}{<true>}{<false>}
\def\bxce@with@star#1{%
  \bxce@with@star@a#1.\bxce@end
}
\def\bxce@with@star@a#1#2\bxce@end{%
  \bxce@cond\ifx*#1\fi
}

%--------------------------------------- CJK package compatibility

%% \bxce@CJK@preproc
\let\bxce@CJK@preproc\@empty
\if d\bxce@engine
\begingroup
  \def\bxce@tmpa#1#2#3#4{% \[bxce@n<?>]\[bxce@o<?>] ?13 ?12
    \gappto\bxce@CJK@preproc{\let#2#3\let#3#1}%
    \ifnum\@tempcnta<224 %2-byte
      \gdef#1##1##2##3{\expandafter
        \ifx\csname u8:#4\string##1\endcsname\relax
        #2##1\else\csname u8:#4\string##1\endcsname\fi}%
    \else\ifnum\@tempcnta<240 %3-byte
      \gdef#1##1##2{\expandafter
        \ifx\csname u8:#4\string##1\string##2\endcsname\relax
        #2##1##2\else\csname u8:#4\string##1\string##2\endcsname\fi}%
    \else %4-byte
      \gdef#1##1##2##3{\expandafter
        \ifx\csname u8:#4\string##1\string##2\string##3\endcsname\relax
        #2##1##2##3\else
        \csname u8:#4\string##1\string##2\string##3\endcsname\fi}%
    \fi\fi
  }
  \@tempcnta=192
  \loop\ifnum\@tempcnta<245
    \lccode`\+=\@tempcnta \lccode`\~=\@tempcnta
    \lowercase{\expandafter\bxce@tmpa\csname bxce@n<+>\expandafter
        \endcsname\csname bxce@o<+>\endcsname~+}%
    \advance\@tempcnta\@ne
  \repeat
\endgroup
\fi

%--------------------------------------- hyperref something

%% add to hook
\appto\pdfstringdefPreHook{%
  \coloremojiLoadAllNames
  \let\coloremoji\bxce@hy@coloremoji
  \let\coloremojiucs\bxce@hy@coloremojiucs
  \def\bxce@@#1;{\bxce@hy@ucs{#1}}%
  \let\bxce@do\bxce@hy@ucs
  \let\bxce@bad\@gobble
}

%% \bxce@hy@NA
\def\bxce@hy@NA{?}

%% \bxce@hy@coloremoji
% \coloremoji in PDF strings. (fully-expandable)
\def\bxce@hy@coloremoji#{%
  \@firstofone  % just put the characters
}

%% \bxce@hy@ucs{<codevalue-hex>}
% Puts the character with the given code value. (fully-expandable)
\def\bxce@hy@ucs#1{%
  % If 'unicode' is on, then \unichar (provided by hyperref)
  % is availble.
  \ifdefined\ifpdfstringunicode \expandafter\ifpdfstringunicode
  \else \expandafter\@secondoftwo
  \fi {\unichar{"#1}}{\bxce@hy@ucs@a{#1}}%
}
\def\bxce@hy@ucs@a#1{%
  % If pxjahyper is loaded, then \Ux can be used.
  \ifdefined\pxDeclarePdfTextCommand
    \ifdefined\Ux \Ux{#1}%
    \else \bxce@hy@NA
    \fi
  \else \bxce@hy@NA
  \fi
}

%% \bxce@hy@coloremojiucs
% \coloremojiucs in PDF strings. (fully-expandable)
\def\bxce@hy@coloremojiucs#{%
  \bxce@hy@coloremojiucs@a
}
\def\bxce@hy@coloremojiucs@a#1{%
  \expandafter\bxce@parse@ccseq\expandafter{\detokenize{#1}}\@firstofone
}

%--------------------------------------- hex strings

%% \bxce@parse@hex{<word>}\CSok\CSerr
% Expands to \CSok{<res>} or \CSerr.
\def\bxce@parse@hex#1{%
  \bxce@parse@hex@a#1@@%
}
\def\bxce@parse@hex@a#1{%<ic>|...
  \bxce@cond\ifx @#1\fi{%
    \bxce@parse@hex@c
  }{%else
    \expandafter\expandafter\expandafter
        \bxce@parse@hex@b\csname bxce@H/#1\endcsname
  }%
}
\def\bxce@parse@hex@b#1#2@#3@{%<ic><iw>@<ow>@|\CSok\CSerr
  \bxce@cond\ifx#1\relax\fi{% unknown char
    \@secondoftwo
  }{%else
    \bxce@parse@hex@a#2@#3#1@%
  }%
}
\def\bxce@parse@hex@c#1{%
  \bxce@cond\if @#1\fi{%
    \bxce@parse@hex@d0@%
  }{\bxce@cond\if0#1\fi{%
    \bxce@parse@hex@c
  }{%else
    \bxce@parse@hex@d#1%
  }}%
}
\def\bxce@parse@hex@d#1@#2#3{%<ow>@\CSok\CSerr
  #2{#1}%
}

%% \bxce@H/*
\begingroup
  \lccode`H=`H
  \def\bxce@tmpa#1\bxce@end{%
    \lowercase{\csgdef{bxce@H/#1}}{#1}%
    \csgdef{bxce@H/#1}{#1}}
  \@tfor\x:=0123456789ABCDEF\do{%
    \expandafter\bxce@tmpa\x\bxce@end}
\endgroup

%--------------------------------------- unparse

%% \bxce@unparse@first{<first>}
% The inverse of \bxce@parse@first@ccseq.
% But here \bxce@O and \bxce@S are illegal.
\def\bxce@unparse@first{%
  \let\bxce@text\@empty
  \let\bxce@T\bxce@unparse@first@T
  \let\bxce@O\bxce@unparse@first@O
  \let\bxce@S\bxce@unparse@first@S
  \let\bxce@E\bxce@unparse@first@E
}
\def\bxce@unparse@first@a#1{%
  \bxce@unparse@first@b#1%
}
\def\bxce@unparse@first@b#1.\bxce@end{%
  \def\bxce@text{#1}%
}
\protected\def\bxce@unparse@first@T.\bxce@end{%
  \expandafter\bxce@unparse@first@a\bxce@text.\bxce@end
}
\protected\def\bxce@unparse@first@O#1{%
  \PackageError\bxce@pkgname
   {Illegal character [\hex{`#1}]}{\@ehc}%
}
\protected\def\bxce@unparse@first@S#1{%
  \bxce@unparse@first@O{\ }%
}
\protected\def\bxce@unparse@first@E#1#2{%
  \unless\ifnum#1=\bxce@cp@evs
    \edef\bxce@text{\bxce@text\space#2}%
  \fi
}

%% \bxce@normalize@text{<text>}
\def\bxce@normalize@text#1{%
  \bxce@expand@text{#1}%
  \edef\bxce@text{\detokenize\expandafter{\bxce@text}}%
  \expandafter\bxce@parse@first\bxce@text\bxce@end
  \expandafter\bxce@unparse@first\bxce@text\bxce@end
}

%% \bxce@normalize@ccseq{<ccseq>}
\def\bxce@normalize@ccseq#1{%
  \bxce@parse@first@ccseq{#1}%
  \expandafter\bxce@unparse@first\bxce@text\bxce@end
}

%--------------------------------------- emoji order list

%% variables
%[bxce@oen/<ccseq>]
\let\bxce@oehead\relax
\let\bxce@oeprev\relax
\let\bxce@oecur\relax
\newcount\bxce@cnto

%% \bxce@geta@ucs
\def\bxce@geta@ucs{3013}
\AtBeginDocument{% geta should stay invalid
  \csundef{bxce@oen/\bxce@geta@ucs}%
}

%%<+> \bxcoloremojiDeclareOrder{<ccseq>,...}
\@onlypreamble\bxcoloremojiDeclareOrder
\newcommand*\bxcoloremojiDeclareOrder[1]{%
  \begingroup
    \@for\bxce@oecur:=#1\do{%
      \bxce@normalize@ccseq\bxce@oecur
      \let\bxce@oecur\bxce@text
      \ifx\bxce@oecur\@empty
      \else\ifcsname bxce@oen/\bxce@oecur\endcsname
        \PackageError\bxce@pkgname
         {Already registered in some emoji order list:\MessageBreak
          \space\space[\bxce@oecur]}%
         {\@eha}%
      \else
        \ifx\bxce@oehead\relax
          \let\bxce@oehead\bxce@oecur
        \else
          \global\cslet{bxce@oen/\bxce@oeprev}\bxce@oecur
        \fi
        \let\bxce@oeprev\bxce@oecur
      \fi\fi}%
    \unless\ifx\bxce@prev\relax
      \global\cslet{bxce@oen/\bxce@oeprev}\bxce@oehead
    \fi
  \endgroup
}

%%<+> \bxcoloremojiUndeclareOrder{<ccseq>,...}
\@onlypreamble\bxcoloremojiUndeclareOrder
\newcommand*\bxcoloremojiUndeclareOrder[1]{%
  \begingroup
    \@for\bxce@oecur:=#1\do{%
      \bxce@normalize@ccseq\bxce@oecur
      \let\bxce@oecur\bxce@text
      \ifx\bxce@oecur\@empty
      \else\ifcsname bxce@oen/\bxce@oecur\endcsname
        \global\csundef{bxce@oen/\bxce@oecur}%
      \fi\fi}%
  \endgroup
}

%%<+> \bxcoloremojiTestOrder{<count>}{<ccseq_0>}
\newcommand*\bxcoloremojiTestOrder[2]{%
  \begingroup
    \bxce@normalize@ccseq{#2}%
    \typeout{Order for <\bxce@text>:}%
    \ifcsname bxce@oen/\bxce@text\endcsname
      \let\bxce@oecur\bxce@text
      \bxce@cnto=#1\relax
      \@whilenum{\bxce@cnto>\z@}\do{%
        \message{<\bxce@oecur>}%
        \coloremojiucs{\bxce@oecur}\space
        \letcs\bxce@oecur{bxce@oen/\bxce@oecur}%
        \ifdefined\bxce@oecur \advance\bxce@cnto\m@ne
        \else \bxce@cnto\z@
        \fi}%
      \unskip\typeout{done}%
    \else \typeout{(not declared)}%
    \fi
  \endgroup
}

%--------------------------------------- pifont-like functions

%%<*> \coloremojifill{<text>}
\newcommand*{\coloremojifill}{%
  \bxce@coloremojifill{\coloremoji*}%
}
%%<*> \coloremojiucsfill{<ccseq>}
\newcommand*{\coloremojiucsfill}{%
  \bxce@coloremojifill{\coloremojiucs*}%
}
\def\bxce@coloremojifill#1#2{%
  \leavevmode
  \leaders\hbox{\kern.375\bxce@unit{#1{#2}}\kern.375\bxce@unit}\hfill
  \kern\z@
}

%%<*> \coloremojiline{<text>}
\newcommand*{\coloremojiline}{%
  \bxce@coloremojiline{\coloremoji*}%
}
%%<*> \coloremojiucsline{<ccseq>}
\newcommand*{\coloremojiucsline}{%
  \bxce@coloremojiline{\coloremojiucs*}%
}
\def\bxce@coloremojiline#1#2{%
  \par\noindent\hspace{0.5in}%
  \bxce@coloremojifill{#1}{#2}%
  \hspace{0.5in}\kern\z@
  \par
}

%%<*> \begin{coloremojilist}{<text>}
\newenvironment*{coloremojilist}[1]{%
  \begin{list}{\coloremoji*{#1}}{}%
}{%
  \end{list}%
}
%%<*> \begin{coloremojiucslist}{<ccseq>}
\newenvironment*{coloremojiucslist}[1]{%
  \begin{list}{\coloremojiucs*{#1}}{}%
}{%
  \end{list}%
}

%% \bxce@ctrformat
\def\bxce@ctrformat#1#2{%
  \protect\bxce@ctrformat@{#1}{\arabic{#2}}%
}
\def\bxce@ctrformat@#1#2{%
  \begingroup
    \bxce@cnto=#2\relax
    \edef\bxce@oecur{#1}%
    \@whilenum{\bxce@cnto>\z@}\do{%
      \letcs\bxce@oecur{bxce@oen/\bxce@oecur}%
      \unless\ifdefined\bxce@oecur
        \let\bxce@oecur\bxce@geta@ucs
      \fi
      \advance\bxce@cnto\m@ne}%
    \coloremojiucs{\bxce@oecur}%
  \endgroup
}

%%<*> \begin{coloremojiautolist}{<text_0>}
\newenvironment*{coloremojiautolist}[1]{%
  \bxce@normalize@text{#1}%
  \bxce@coloremojiautolist
}{%
  \endlist
}
%%<*> \begin{coloremojiucsautolist}{<ccseq>}
\newenvironment*{coloremojiucsautolist}[1]{%
  \bxce@normalize@ccseq{#1}%
  \bxce@coloremojiautolist
}{%
  \endlist
}
\def\bxce@coloremojiautolist{%
  \unless\ifcsname bxce@oen/\bxce@text\endcsname
    \PackageError\bxce@pkgname
     {Not registered in any emoji order list:\MessageBreak
      \space\space[\bxce@text]}%
     {\@ehc}%
    \let\bxce@text\bxce@geta@ucs
  \fi
  \ifnum\@enumdepth>\thr@@
    \@toodeep
  \else
    \advance\@enumdepth\@ne
    \edef\@enumctr{enum\romannumeral\@enumdepth}%
    \cslet{p@\@enumctr}\@empty
    \csedef{label\@enumctr}{\noexpand\csuse{the\@enumctr}}%
    \csedef{the\@enumctr}{%
      \noexpand\bxce@ctrformat{\bxce@text}{\@enumctr}}%
    \list{\csuse{label\@enumctr}}{%
      \@nmbrlisttrue
      \edef\@listctr{\@enumctr}%
      \setcounter{\@enumctr}{\m@ne}%
      \def\makelabel##1{\hss\llap{##1}}}%
  \fi
}

%% 'coloremojicode' aliases
\newcommand*{\coloremojicodefill}{\coloremojiucsfill}
\newcommand*{\coloremojicodeline}{\coloremojiucsline}
\@ifdefinable{\coloremojicodelist}{%
  \let\coloremojicodelist\coloremojiucslist
  \let\endcoloremojicodelist\endcoloremojiucslist
}
\@ifdefinable{\coloremojicodeautolist}{%
  \let\coloremojicodeautolist\coloremojiucsautolist
  \let\endcoloremojicodeautolist\endcoloremojiucsautolist
}

%--------------------------------------- emoji order database

%\bxcoloremojiDeclareOrder{% snowman (for test)
%  2603, 26C4}
\bxcoloremojiDeclareOrder{% zodiac symbol
  2648, 2649, 264A, 264B, 264C, 264D, 264E, 264F, 2650, 2651,
  2652, 2653}
\bxcoloremojiDeclareOrder{% playing card suit
  2660, 2665, 2666, 2663}
%\bxcoloremojiDeclareOrder{% color heart (provisional)
%  2764, 1F49B, 1F49A, 1F499, 1F49C}% cf. 1F9E1
%\bxcoloremojiDeclareOrder{% color circle (provisional)
%  1F534, 1F535}
\bxcoloremojiDeclareOrder{% clock face
  1F550, 1F551, 1F552, 1F553, 1F554, 1F555, 1F556, 1F557, 1F558, 1F559,
  1F55A, 1F55B}
\bxcoloremojiDeclareOrder{% keycap number
  0030 20E3, 0031 20E3, 0032 20E3, 0033 20E3, 0034 20E3,
  0035 20E3, 0036 20E3, 0037 20E3, 0038 20E3, 0039 20E3, 1F51F}

%--------------------------------------- done
\endinput
%% EOF
